#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/home/models.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import json
from django.db import models
from common.log import logger

class UserAppsManager(models.Manager):

    def add_app(self, userapp, appcode):
        """
        收藏应用
        """
        try:
            apps = json.loads(userapp.apps)
            if appcode not in apps:
                apps.append(appcode)
            userapp.apps = json.dumps(apps)
            userapp.save()
            return True
        except Exception as e:
            logger.error(u'add_app error: %s' % e)
            return False

    def del_app(self, userapp, appcode):
        """
        取消收藏
        """
        try:
            if not userapp:
                return False
            apps = json.loads(userapp.apps)
            if appcode in apps:
                apps.remove(appcode)
            userapp.apps = json.dumps(apps)
            userapp.save()
            return True
        except Exception as e:
            logger.error(u'add_app error: %s' % e)
            return False


class UserApps(models.Model):
    """
    用户收藏应用信息
    """
    username = models.CharField(u'用户名称', max_length=128, unique=True)
    apps = models.TextField(u'应用列表', default='', blank=True, null=True, help_text=u'格式：json数据[code1,code2,code3]')
    objects = UserAppsManager()

    def __unicode__(self):
        return '%s' % self.username

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'paas_userapps'
        verbose_name = u'用户收藏应用'
        verbose_name_plural = u'用户收藏应用'


class UserSettings(models.Model):
    """
    首页上用户自定义的应用列表
    """
    username = models.CharField(u'用户名称', max_length=128, unique=True)
    apps = models.TextField(u'应用列表', default='', blank=True, null=True, help_text=u'格式：json数据[code1,code2,code3]')

    def __unicode__(self):
        return '%s' % self.username

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'paas_usersettings'
        verbose_name = u'用户自定义的应用列表'
        verbose_name_plural = u'用户自定义的应用列表'


class UsefulLinks(models.Model):
    """
    常用链接
    """
    name = models.CharField(u'名称', max_length=128)
    link = models.CharField(u'链接', max_length=128)

    def __unicode__(self):
        return '%s' % self.name

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'paas_usefullinks'
        verbose_name = u'常用链接'
        verbose_name_plural = u'常用链接'
